// pages/my/purchase/together/shareTogether.js
const app = getApp()
Page({
  data: {
    agentId:'',
    accountId:'',
    orderId:'',
    stockSkuId:'',
    goods:[],
    togetherId:'',
    goodsType:true,
    isCompany:true,
    goodsPriceShow:true,
    hour:'',
    minutes:'',
    seconds:'',
    status:''
  },
  onLoad: function (options) {
    app.getMemInfo().then(res=>{
      this.setData({
        agentId:res.data.agentId,
        accountId:res.data.id
      })
    })
    let orderId = options.orderId,
    stockSkuId = options.stockSkuId
    // let orderId = 489,
    //     stockSkuId = 54
    this.setData({
      orderId,
      stockSkuId
    })
    this.getOrderDetail(orderId,stockSkuId)

  },
  getOrderDetail(orderId,stockSkuId){
    let data = {
      orderId,
      stockSkuId
    }
    let that = this
    console.log(data)
    app.request('wx_app/together/getShareByOrderId',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        let goods = []
        goods={
          url:res.data.logoUrl,
          color:res.data.tagColor,
          companyName:res.data.companyName,
          name:res.data.productName,
          price:res.data.price,
          actualPrice:res.data.price,
          type:res.data.versionValues,
          head:res.data.headPortrait,
          head2:res.data.headPortrait2,
          time:res.data.togetherCreateTime
        }
        this.setData({
          goods,
          togetherId:res.data.togetherId,
          status:res.data.status //0正常1关闭
        })
        setInterval(function () {
          that.timeControl(goods.time)
        }, 1000)
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  timeControl(time){
    let end = new Date(parseInt(time + 86400000))
    let now = new Date()
      //总秒数
    let totalSeconds = parseInt((end - now) / 1000),
    //小时数
    hour = Math.floor(totalSeconds / (60 * 60)),
    //取余
    modulo = totalSeconds % (60 * 60),
    //分钟
    minutes = Math.floor(modulo / 60),
    //秒
    seconds = modulo % 60;
    this.setData({
      hour,
      minutes,
      seconds
    })
  },
  togetherBuy(){
    let that = this
    wx.getStorage({
      key:'memInfo',
      success(res){
        console.log(res)
        let data = {
          agentId:that.data.agentId,
          accountId:that.data.accountId,  
          stockSkuId:that.data.stockSkuId,  
          togetherId:that.data.togetherId
        }
        console.log(data)
        app.request('wx_app/together/saveAgentOrderByCart',data,'GET').then(res=>{
          console.log(res)
          if (res.code==1) {
            let orderId = res.data.id
            wx.navigateTo({
              url:'../confirmOrder?orderId=' + orderId + '&applyType=0' + '&type=1' //0批发1拼单
            })
        }else{
          app.errorMsg(res.message)
        }
      })
          console.log('用户已登录')
      },
      fail(res){
        console.log(res.errMsg+':用户未登录')
        wx.navigateTo({
          url:'../../../index/index'
        })
      }
    })

  }
})